﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ras2Logic
{
    /// <summary>
    /// Klasinn PerfomrerController
    /// </summary>
    public class PerformerController
    {
        #region Member breytur

        /// <summary>
        /// Memberbreyta sem inniheldur tengingu við databaseinn.
        /// </summary>
        private Ras2DBDataContext m_context = new Ras2DBDataContext();

        #endregion

        #region Smiðir

        #endregion

        #region Get / Set föll

        #endregion

        #region Private föll

        #endregion

        #region Public föll

        /// <summary>
        /// Fall sem sækir alla flytjendur.
        /// </summary>
        /// <returns>Skilar safni af flytjendum</returns>
        public IEnumerable<Performer> GetAllPerformers()
        {
            //Þetta skilar öllu úr Performers töfluni. a
            var result = from performer in m_context.Performers
                         orderby performer.Name ascending
                         select performer;
            return result;
        }

        /// <summary>
        /// Fall sem sækir flytjanda eftir Performer ID.
        /// </summary>
        /// <param name="nID">Tekur inn ID flytjanda</param>
        /// <returns>Skilar flytjanda af tegund Performer</returns>
        public Performer GetPerformerById(int nID)
        {
            return (from a in m_context.Performers
                    where a.ID == nID
                    select a).SingleOrDefault();
        }

        /// <summary>
        /// Fall sem uppfærir flytjanda.
        /// </summary>
        /// <param name="s">Tekur inn flytjanda af tegund Performer</param>
        public void UpdatePerformer(Performer s)
        {
            Performer original = (from a in m_context.Performers
                                where a.ID == s.ID
                                select a).SingleOrDefault();

            if (original != null)
            {
                original.ID = s.ID;
                original.Name = s.Name;

                m_context.SubmitChanges();
            }
        }

        /// <summary>
        /// Fall sem bætir við nýjum flytjanda.
        /// </summary>
        /// <param name="s">Tekur inn flytjanda af tegund Performer</param>
        public void AddPerformer(Performer s)
        {
            m_context.Performers.InsertOnSubmit(s);
            m_context.SubmitChanges();
        }

        #endregion

    }
}
